package csdn.jihe.li07;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class TestTree {
    public static void main(String[] args) {
        //1.创建一个实现Comparator接口的对象
        Comparator<Student> comparator=new Comparator<Student>() {
            //通过匿名内部类实现Comparator接口功能
            //2.重写compara()
            @Override
            public int compare(Student o1, Student o2) {
                int num;
                //age升序
                num=o1.getAge()-o2.getAge();
                if(num==0){
                    //name降序
                    num= o2.getName().compareTo(o1.getName());
                    if(num==0){
                        //sex升序
                        num=o1.getSex().compareTo(o2.getSex());
                    }
                }
                return num;
            }

        };
        //3.创建一个TreeSet类对象，将比较器对象作为参数传递给构造函数
        Set<Student> tree=new TreeSet<>(comparator);

        Student s1=new Student("李霄",18,"男");
        Student s2=new Student("儿霄",28,"购物袋");
        Student s3=new Student("小霄",88,"女");
        tree.add(s1);
        tree.add(s2);
        tree.add(s3);
        System.out.println(tree);

    }
}
